package com.johnsoft.pph;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Pngout {
	
	public Pngout(File f) {
		try {
			//System.out.println("Defalting " + f.getAbsolutePath());
			Process pngout = new ProcessBuilder("pngout.exe", f.getAbsolutePath()).start();
			InputStream is = pngout.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String line;
			boolean nextLineIsResult = false;
	
			while ((line = br.readLine()) != null) {
				//System.out.println(line);
				if(line.startsWith("Out:")) {
					nextLineIsResult = true;
				} else if (nextLineIsResult) {
					if(line.startsWith("Chg:")) {
					    line = line.replace(" ", "");
					    line = line.substring(5);
					    line = line.substring(0, line.indexOf("bytes"));
					    System.out.println("Deflated " + f.getName() + " with " + line + " bytes");
					}
				}
			}

		} catch (IOException e) {
			System.err.println("RESTART THE CREATION! Deflating of an image failed!");
			e.printStackTrace();
		}
	}

}
